package cn.edu.ecust.service.user;

import cn.edu.ecust.common.constant.HttpRespCode;
import cn.edu.ecust.common.util.Resp;
import cn.edu.ecust.dto.UserDTO;
import cn.edu.ecust.mapper.user.UserMapper;
import cn.edu.ecust.po.user.User;
import cn.edu.ecust.vo.request.LoginUserVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author cjj
 * @date 2023/09/03 09:10
 **/
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public List<User> getUsers(){
        return userMapper.selectUsers();
    }

    public Resp login(LoginUserVO loginUserVO){
        User user = userMapper.selectUserByName(loginUserVO.getUsername());
        if(ObjectUtil.isNull(user)){
            return new Resp("506","用户名不存在");
        }
        if(!loginUserVO.getPassword().equals(user.getPassword())){
            return new Resp("507","输入密码错误");
        }
        /*UserDTO userDTO = BeanUtil.copyProperties(user,UserDTO.class);
        Map<String,Object> userMap = BeanUtil.beanToMap(userDTO,new HashMap<>(), CopyOptions.create()
                .setIgnoreNullValue(true)
                .setFieldValueEditor((fieldName,fieldValue) -> fieldValue.toString()));
        String token = UUID.randomUUID().toString(true);
        stringRedisTemplate.opsForHash().putAll(token,userMap);
        stringRedisTemplate.expire(token,30, TimeUnit.MINUTES);
        return new Resp(HttpRespCode.SUCCESS,token);*/
        return  new Resp("200", "请求成功");
    }
}
